package com.study.spmongodb.wwj.thread2.char15;

import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/8 22:49
 */
public class MessageHandler {
    private final static Random random = new Random(System.currentTimeMillis());
    private final static Executor executor = Executors.newFixedThreadPool(5);

    public void requestNew(Message message) {
        executor.execute(() -> {
            final String value = message.getValue();
            try {
                Thread.sleep(random.nextInt(1000));
                System.out.println(Thread.currentThread().getName() + " " + value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public void request(Message message) {
        new Thread(() -> {
            final String value = message.getValue();
            try {
                Thread.sleep(random.nextInt(1000));
                System.out.println(Thread.currentThread().getName() + " " + value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
